package com.shujia.scala;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Demo24JavaWordCount {
    public static void main(String[] args) throws IOException {
        HashMap<String, Integer> hMap = new HashMap<>();

        BufferedReader br = new BufferedReader(new FileReader("data/words.txt"));

        String line;
        while ((line = br.readLine()) != null) {
            // line相当于每一行数据
            for (String word : line.split(",")) {
                if (hMap.containsKey(word)) {
                    // 先将word对应的次数取出来
                    Integer i = hMap.get(word);
                    // 给word对应的次数加1
                    hMap.put(word, i + 1);
                } else {
                    // map中不存在 则将word添加至map中并将value置为1
                    hMap.put(word, 1);
                }
            }
        }
        for (Map.Entry<String, Integer> entry : hMap.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "," + value);
        }
        br.close();
    }
}
